#ifndef __DSTH_SERVER_BOOT_STRAP_H__
#define __DSTH_SERVER_BOOT_STRAP_H__

#include <map>
#include <assert.h>
#include "EventLoop.h"
#include "Connection.h"

namespace dsth {
class ReactorThreadPool;
class Acceptor;
class ServerBootstrap {
public:
    ServerBootstrap(EventLoop* loop, const InetSocketAddress& addr);
    ServerBootstrap(ReactorThreadPool *pool, const InetSocketAddress& addr);
    ~ServerBootstrap();

    void SetReactorThreadPool(ReactorThreadPool *pool) { pool_ = pool; }
    void Run();
    void SetOnConnection(const Connection::ConnectionHandler& handler) { conn_handler_ = handler; }
    void SetOnDisConnection(const Connection::ConnectionHandler& handler) { dis_conn_handler_ = handler; }
    void SetOnMessage(const Connection::MessageHandler& handler) { msg_handler_ = handler; }
    void SetOnWriteCompleted(const Connection::ConnectionHandler& handler) { write_completed_handler_ = handler;}

private:
    // Can't invoke copy constructor or assignment operator
    ServerBootstrap(const ServerBootstrap&);
    void operator=(const ServerBootstrap&);

    void OnNewConnection(int fd, const InetSocketAddress& peer_addr);
    void OnCloseConnection(Connection* conn);
    EventLoop* GetNextEventLoop();

    typedef std::map<int, Connection*> ConnectionMap;
    ConnectionMap conn_map_;
    EventLoop* loop_;
    // Don't expose Acceptor.h to user
    std::auto_ptr<Acceptor> acceptor_;
    int conn_id_;
    ReactorThreadPool* pool_;

    Connection::ConnectionHandler conn_handler_;
    Connection::ConnectionHandler dis_conn_handler_;
    Connection::ConnectionHandler write_completed_handler_;
    Connection::MessageHandler msg_handler_;
};

} // namespace
#endif // __DSTH_SERVER_BOOT_STRAP_H__
